package com.sm.service;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sm.dao.*;
import com.sm.minio.Minio;
import com.sm.model.Dynamic;
import com.sm.model.DynamicComment;
import com.sm.util.PageResult;
import com.sm.vo.VodVO;
import com.sm.vod.AliyunUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.*;

import static com.sm.minio.Minio.BUCKET_SHARE_VIDEO;


@Service
public class DynamicService {
    @Autowired
    private DynamicMapper dynamicMapper;
    @Autowired
    private DynamicCommentMapper dynamicCommentMapper;
    @Autowired
    private LikeDynamicMapper likeDynamicMapper;
    @Autowired
    private DynamicCollectMapper dynamicCollectMapper;

    @Autowired
    private UserMapper userMapper;

    //查询动态
    public PageResult selectdynamic(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        Page<Dynamic> dynamics = dynamicMapper.selectDynamic();
        List<Dynamic> dynamicList = dynamics.getResult();
        for (Dynamic dynamic : dynamicList) {
            ArrayList<String> strings = new ArrayList<>();
            if (StrUtil.isNotEmpty(dynamic.getUrl())) {
                String[] split = dynamic.getUrl().split(",");
                for (String s : split) {
                    strings.add(s);
                }
                //添加评论次数
                int i = dynamicCommentMapper.selectCommentNum(dynamic.getDynamicId());
                dynamic.setCommentNum(i);
                dynamic.setUrlList(strings);
            }
        }
        return new PageResult(Long.valueOf(dynamicList.size()), dynamicList);

    }

    public Dynamic selectDynamicByDaynamicId(Integer dynamicId) {
        Dynamic dynamic = dynamicMapper.selectDynamicById(dynamicId);
        return dynamic;
    }
    //根据动态的ID拉取此动态的评论回复记录(一级，二级)
    public List<Map> selectCommentByDynamic(Integer dynamicId) {
        List<Map> resultList = new ArrayList<>();
        List<DynamicComment> commentList = dynamicCommentMapper.selectCommentByDynamicFirst(dynamicId);
        for (DynamicComment dy : commentList) {
            List<DynamicComment> sonComments = dynamicCommentMapper.selectCommentByDynamicSecond(dynamicId, dy.getFatherComment());
            HashMap<String, Object> map = new HashMap<>();
            map.put("comment" + dy.getFatherComment(), dy);
            map.put("comment" + dy.getFatherComment() + "的子评论", sonComments);
            resultList.add(map);
        }
        return resultList;
    }

    //根据动态的ID删除此条动态（假删除不再显示）同时删除所有的评论和回复点赞记录
    public void deleteCommentByDynamic(Integer dynamicId) {
        dynamicCommentMapper.deleteCommentByDynamic(dynamicId);
        dynamicMapper.deleteByDynamic(dynamicId);
        likeDynamicMapper.deleteComment(dynamicId);

    }

    public int addDynamicPost(Dynamic dynamic) throws Exception {
        dynamic.setCreateTime(new Date());
        List<String> urlList = dynamic.getUrlList();
        String join = String.join(",", urlList);
        dynamic.setUrl(join);
        //视频
        if (dynamic.getContentType().equals(1)) {
            String videoId = dynamic.getVideoId();
            if (StrUtil.isNotEmpty(videoId)) {
                VodVO playInfo = AliyunUpload.getPlayInfo(videoId);
                dynamic.setCover(playInfo.getCoverUrl());
                dynamic.setUrl(playInfo.getLdUrl());
            }
        }
        int i = dynamicMapper.addDynamicPost(dynamic);
        return i;
    }

    /**
     * 查询动态详情
     *
     * @param dynamicId
     * @return
     */
    public Dynamic selectdynamicById(Integer dynamicId) {
        return dynamicMapper.selectDynamicById(dynamicId);
    }


    public int updateDynamic(Dynamic dynamci) {
        int i = dynamicMapper.updateDynamic(dynamci);
        return i;
    }

    public int updateCommentNum(Dynamic dynamic) {
        int num = dynamicMapper.updateCommentNum(dynamic);
        return num;
    }
}
